﻿

Imports System.Net
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Security.Cryptography
Imports Microsoft.Win32
' verweis nötig auf System.ServiceModel.Web und System.Runtime.Serialization
Imports System.Runtime.Serialization.Json

Public Class MyJson

    Public Shared Function SerializeJson(Of T)(obj As T) As String
        Dim serializer As New DataContractJsonSerializer(obj.[GetType]())
        Dim ms As New MemoryStream()
        serializer.WriteObject(ms, obj)
        Dim retVal As String = Encoding.UTF8.GetString(ms.ToArray())
        Return retVal
    End Function
    Public Shared Function DeserializeJson(Of T)(json As String) As T
        Dim obj As T = Activator.CreateInstance(Of T)()
        Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(json))
        Dim serializer As New DataContractJsonSerializer(obj.[GetType]())
        obj = DirectCast(serializer.ReadObject(ms), T)
        ms.Close()
        Return obj
    End Function

End Class
